<cfparam name="resetFilters" default="false">
<cfset isFiltered = false />
<cfset queryParams = StructNew() />

<cfif NOT IsDefined("client.users.Approved") or resetFilters>
	<cfset client.users.Approved = "All">
<cfelseif IsDefined("filterApproved")>
	<cfset client.users.Approved = filterApproved>
</cfif>
<cfif client.users.Approved neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.Approved = client.users.Approved />
</cfif>

<cfif NOT IsDefined("client.users.Archived") or resetFilters>
	<cfset client.users.Archived = "No">
<cfelseif IsDefined("filterArchived")>
	<cfset client.users.Archived = filterArchived>
</cfif>
<cfif client.users.Archived neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.Archived = client.users.Archived />
</cfif>

<!--- <cfif NOT IsDefined("client.users.Region_IDs") or resetFilters>
	<cfif request.authUser.Role eq "Super User">
		<cfset client.users.Region_IDs = "All">
	<cfelseif request.authUser.Role eq "Regional Admin">
		<cfset client.users.Region_IDs = request.authUser.regionID>
	</cfif>
<cfelseif IsDefined("filterRegionID")>
	<cfset client.users.Region_IDs = filterRegionID>
</cfif>
<cfif client.users.Region_IDs neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.Region_IDs = client.users.Region_IDs />
</cfif> --->

<!--- <cfif client.users.Archived eq "All">
	<cfset isFiltered = true />
	<cfset queryParams.Archived = "" />
<cfelseif client.users.Archived eq "Yes">
	<cfset isFiltered = true />
	<cfset queryParams.Archived = "Yes" />
<cfelseif client.users.Archived eq "No">
	<cfset queryParams.Archived = "No" />
</cfif> --->

<cfif NOT IsDefined("client.users.role") or resetFilters>
	<cfset client.users.role = "All">
<cfelseif IsDefined("filterRole")>
	<cfset client.users.role = filterRole>
	<!--- 
		If role filter set to Super User or Regional Admin, set organizationIDs to All in 
		order to avoid returning no results if organizations are filtered.
		
		Beware this requires the filter logic for organizationIDs to fall after this set
	
	<cfif filterRole eq "Super User" or filterRole eq "Regional Admin">
		<cfset filterOrganizationIDs = "All" />
	</cfif>--->
	<!--- 
		If role filter set to Super User, set regionIDs to All in 
		order to avoid returning no results if regions are filtered.
		
		Beware this requires the filter logic for regionIDs to fall after this set
	
	<cfif filterRole eq "Super User">
		<cfset filterRegionIDs = "All" />
	</cfif>--->
</cfif>
<cfif client.users.role neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.Role = client.users.role />
</cfif>

<cfif NOT IsDefined("client.users.organizationIDs") or resetFilters>
	<cfset client.users.organizationIDs = "All">
<cfelseif IsDefined("filterOrganizationIDs")>
	<cfset client.users.organizationIDs = filterOrganizationIDs>
</cfif>
<cfif client.users.organizationIDs neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.organizationIDs = client.users.organizationIDs />
</cfif>

<cfif IsUserInRole("Regional Admin")>
	<cfset client.users.regionIDs = request.authUser.regionID />
<cfelseif NOT IsDefined("client.users.regionIDs") or resetFilters>
	<cfset client.users.regionIDs = "All">
<cfelseif IsDefined("filterRegionIDs")>
	<cfset client.users.regionIDs = filterRegionIDs>
</cfif>
<cfif client.users.regionIDs neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.regionIDs = client.users.regionIDs />
</cfif>

<!--- <cfif IsUserInRole("Super User")>
	<cfif NOT IsDefined("client.users.regionIDs") or resetFilters>
		<cfset client.users.regionIDs = "All">
	<cfelseif IsDefined("filterRegionIDs")>
		<cfset client.users.regionIDs = filterRegionIDs>
	</cfif>
<cfelseif IsUserInRole("Regional Admin")>
	<cfset client.users.Region_ID = request.authUser.regionID>
</cfif>
<cfif client.users.regionIDs neq "All">
	<cfset isFiltered = true />
	<cfset queryParams.regionIDs = client.users.regionIDs />
</cfif> --->
<cfwddx action="cfml2wddx" input="#queryParams#" output="client.users.queryParamsWDDX" />

<cfscript>
	userGateway = new assets.calgrants.com.gateway.UserGateway();
	users = userGateway.query(argumentCollection = queryParams);
	organizationGateway = new assets.calgrants.com.gateway.OrganizationGateway();
	organizations = organizationGateway.query(regionIDs = IIf(IsUserInRole("Regional Admin"), request.authUser.regionID, ""), Organization_ID = IIf(IsUserInRole("Organizer") OR IsUserInRole("Site Contact"), request.authUser.organizationID, ""));
	storedProcedureGateway = new assets.calgrants.com.gateway.StoredProcedureGateway();
	regions = storedProcedureGateway.call("getDistinctRegions");
</cfscript>

<cfoutput>

	<h3>Users<cfif isFiltered> <small><em>filtered</em></small></cfif></h3>

	<!--- <p>
		<a href="#script_name#?view=editUser&User_ID=0" class="btn">New&nbsp;user</a> <a id="filterLink" href="##" class="btn" onclick="toggleFilter('filter')">Show filters</a><!--- <cfif isFiltered> <a href="#script_name#?view=users&resetFilters=true" class="btn">Clear filters</a></cfif> ---><cfif users.RecordCount gt 0> <a href="?action=export&collection=users" class="btn" target="_new">Export</a></cfif>
		<span class="pull-right"><strong>Count:</strong> #users.RecordCount#</span>
	</p> --->
	
	<div class="row-fluid" style="margin-bottom: 10px;">
		<div class="span12">
			<a href="#script_name#?view=editUser&User_ID=0" class="btn">New&nbsp;user</a>
			<a id="filterLink" href="##" class="btn" onclick="toggleFilter('filter')">Show filters</a>
			<div class="btn-group">
				<button class="btn dropdown-toggle" data-toggle="dropdown">Actions</button>
				<button class="btn dropdown-toggle" data-toggle="dropdown"><span class="caret"></span></button>
				<ul class="dropdown-menu">
					<cfif users.RecordCount gt 0>
						<li><a href="?action=export&collection=users" target="_new">Export</a></li>
					<cfelse>
						<li><a href="##">None - no records available with filter settings</a></li>
					</cfif>
				</ul>
			</div>
			<span class="pull-right"><strong>Count:</strong> #users.RecordCount#</span>
		</div>
	</div>

	<cfform name="usersForm" id="usersForm" action="#script_name#" method="post">
		<cfinput type="hidden" name="view" value="#view#">
		
		<div id="filter" class="alert alert-info alert-block" style="display: none;">
			<h4>Filters</h4>
			<table cellpadding="6">
				<tr valign="top">
					<cfif IsUserInRole("Super User")>
						<td>
							<label>Regions</label>
							<cfselect name="filterRegionIDs" size="7" multiple="true" required="yes" message="Region filter is required" style="width: 150px;">
								<option value="All"<cfif client.users.regionIDs eq "All"> selected</cfif>>All</option>
								<cfloop query="regions">
								<option value="#Region_ID#"<cfif ListFind(client.users.regionIDs, Region_ID)> selected</cfif>>#Region_ID#</option>
								</cfloop>
							</cfselect>
							<cfinput type="hidden" name="filterRegionID" value="All">
						</td>
					<cfelse>
						<cfinput type="hidden" name="filterRegionIDs" value="#request.authUser.regionID#">
						<cfinput type="hidden" name="filterRegionID" value="All">
						<td>
							<label>Region</label>
							<div style="width: 100px; color: black;">#request.authUser.regionID#</div>
						</td>
					</cfif>
					<td>
						<label>Organizations</label>
						<cfselect name="filterOrganizationIDs" size="7" multiple="true" required="yes" message="Organization filter is required" style="width: auto;">
							<option value="All"<cfif client.users.organizationIDs eq "All"> selected</cfif>>All</option>
							<cfloop query="organizations">
							<option value="#Organization_ID#"<cfif ListFind(client.users.organizationIDs, Organization_ID)> selected</cfif>>#Organization# - #City#</option>
							</cfloop>
						</cfselect>
					</td>
					<td>
						<label>Role</label>
						<cfselect name="filterRole" size="1" required="yes" message="Role filter is required" style="width: 150px;">
							<option value="All"<cfif client.users.role eq "All"> selected</cfif>>All</option>
							<cfif IsUserInRole("Super User")>
								<option value="Super User"<cfif ListFind(client.users.role, "Super User")> selected</cfif>>Super User</option>
								<option value="Regional Admin"<cfif ListFind(client.users.role, "Regional Admin")> selected</cfif>>Regional Admin</option>
							</cfif>
							<option value="Organizer"<cfif ListFind(client.users.role, "Organizer")> selected</cfif>>Organizer</option>
							<option value="Site Contact"<cfif ListFind(client.users.role, "Site Contact")> selected</cfif>>Site Contact</option>
						</cfselect>
					</td>
					<td>
						<label>Status</label>
						<cfselect name="filterApproved" size="1" multiple="false" required="yes" message="Status filter is required" style="width: 150px;">
							<option value="All"<cfif client.users.approved eq "All"> selected</cfif>>All</option>
							<option value="Yes"<cfif client.users.approved eq "Yes"> selected</cfif>>Approved</option>
							<option value="No"<cfif client.users.approved eq "No"> selected</cfif>>Not approved</option>
						</cfselect>
					</td>
					<td>
						<label>Archived</label>
						<cfselect name="filterArchived" size="1" multiple="false" required="yes" message="Archived is required" style="width: 150px;">
							<option value="All"<cfif client.users.archived eq "All"> selected</cfif>>All</option>
							<option value="Yes"<cfif client.users.archived eq "Yes"> selected</cfif>>Yes</option>
							<option value="No"<cfif client.users.archived eq "No"> selected</cfif>>No</option>
						</cfselect>
					</td>
				</tr>
				<tr>
					<td colspan="5">
						<button type="submit" class="btn btn-info">Apply</button> <a href="#script_name#?view=users&resetFilters=true" class="btn btn-info">Clear</a>
					</td>
				</tr>
			</table>
		</div>

		<cfif users.RecordCount gt 0>

			<table id="usersList" style="display: none; width: 100%">
				<thead>
					<th>ID</th>
					<th>Organization</th>
					<cfif IsUserInRole("Super User")>
						<th>Region</th>
					</cfif>
					<th>Name</th>
					<th>Role</th>
					<th>E-mail</th>
					<th>Workshops</th>
					<th>Orders</th>
					<th>Status</th>
				</thead>
				<tbody>
					<cfloop query="users">
						<tr>
							<td><a href="#script_name#?view=editUser&User_ID=#User_ID#">#User_ID#</a></td>
							<td>
								<cfif Organization_ID gt 0>
									<cfif Organization_Deleted>
										<font color="red">#Organization#</font>
									<cfelseif Organization_Archived>
										<font color="silver">#Organization#</font>
									<cfelseif NOT Organization_Approved>
										<font color="orange">#Organization#</font>
									<cfelseif Organization_Approved>
										#Organization#
									</cfif>
								</cfif>
							</td>
							<cfif IsUserInRole("Super User")>
								<td>#Region_ID#</td>
							</cfif>
							<td>#First_Name# #Last_Name#</td>
							<td>#Role#<cfif Role eq "Regional Admin"> (#Region_IDs#)</cfif></td>
							<td>#Email#</td>
							<td>#Workshop_Count#</td>
							<td>#Order_Count#</td>
							<td>
								<cfif Deleted>
									<font color="red">Deleted</font>
								<cfelseif Archived>
									<font color="silver">Archived</font>
								<cfelseif NOT Approved>
									<font color="orange">Not Approved</font>
								<cfelseif Approved>
									Approved
								</cfif>
							</td>
						</tr>
					</cfloop>
				</tbody>
			</table>
			
			<script type="text/javascript">
				$(function() {
					$("##usersList").kendoGrid({
						sortable: {
							mode: "single",
							allowUnsort: false
						}
						//filterable: true,
						//columnMenu: true			
					})
					$("##usersList").show();
				})
			</script>
			
		<cfelse>
		
			<div class="alert alert-info notFound">No users</div>
	
			<script type="text/javascript" language="javascript">
			<!--
				$(function() {
					$("##filter").show();
				});
			// -->
			</script>
			
		</cfif>

	</cfform>

</cfoutput>

<!--- <cfdump var="#queryParams#"> --->